/**
 * 
 */
package myTeam.timeShop.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import myTeam.timeShop.bean.TbDemand;
import myTeam.timeShop.bean.TbUser;
import myTeam.timeShop.entity.Demand;
import myTeam.timeShop.service.CharService;
import myTeam.timeShop.service.CommentService;
import myTeam.timeShop.service.DemandService;
import myTeam.timeShop.service.UserService;
import myTeam.timeShop.util.MyMapBuilder;
import myTeam.timeShop.util.UserFaceUploadUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author jiangjintai
 *
 */
@Controller
@RequestMapping("/client/demand")
@Api(description = "需求版块", value = "需求版块")
public class DemandController {
	@Autowired
	CharService charService;
	@Autowired
	UserService userService;
	@Autowired
	CommentService commentService;
	@Autowired
	DemandService demandService;

	/**
	 * 发布需求 jiangjintai 2016年3月27日
	 * 
	 * @param demandContent
	 * @param demandType
	 * @param demandPay
	 * @param demandPhoto1
	 * @param demandPhoto2
	 * @param demandPhoto3
	 * @param demandPhoto4
	 * @param demandPhoto5
	 * @param demandPhoto6
	 * @param demandTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addDemand")
	@ApiOperation(value = "添加需求", httpMethod = "POST")
	public String addDemand(
			@ApiParam(value = "需求内容", name = "demandContent", required = true) @RequestParam("demandContent") String demandContent,
			@ApiParam(value = "需求类型", name = "demandType", required = true) @RequestParam("demandType") String demandType,
			@ApiParam(value = "需求支付金额或交换的时间，时间务必为整数", name = "demandPay", required = true) @RequestParam("demandPay") String demandPay,
			@ApiParam(value = "需求标题", name = "demandTitle", required = true) @RequestParam("demandTitle") String demandTitle,
			HttpServletRequest request,
			@ApiParam(value = "用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId,
			@ApiParam(value = "图片1", name = "demandPhoto1", required = false) @RequestParam("demandPhoto1") MultipartFile demandPhoto1,
			@ApiParam(value = "图片2", name = "demandPhoto2", required = false) @RequestParam("demandPhoto2") MultipartFile demandPhoto2,
			@ApiParam(value = "图片3", name = "demandPhoto3", required = false) @RequestParam("demandPhoto3") MultipartFile demandPhoto3,
			@ApiParam(value = "图片4", name = "demandPhoto4", required = false) @RequestParam("demandPhoto4") MultipartFile demandPhoto4,
			@ApiParam(value = "图片5", name = "demandPhoto5", required = false) @RequestParam("demandPhoto5") MultipartFile demandPhoto5,
			@ApiParam(value = "图片6", name = "demandPhoto6", required = false) @RequestParam("demandPhoto6") MultipartFile demandPhoto6) {

		TbDemand demand = new TbDemand();
		demand.setDemandContent(demandContent);
		demand.setDemandType(Integer.valueOf(demandType));
		demand.setDemandPay(new BigDecimal(demandPay));
		demand.setDemandTitle(demandTitle);
		demand.setDemandAcceptTime(null);
		demand.setDemandCancelTime(null);
		demand.setDemandCommentCount(0);
		demand.setDemandFinishTime(null);
		demand.setDemandLikeCount(0);
		demand.setDemandStatus(0);
		demand.setDemandTime(new Date());
		TbUser user = userService.get(filterUserId);
		demand.setTbUserByDemandUserId(user);

		List<MultipartFile> photoList = new ArrayList<MultipartFile>();
		List<String> photoPathList = new ArrayList<String>();
		photoList.add(demandPhoto1);
		photoList.add(demandPhoto2);
		photoList.add(demandPhoto3);
		photoList.add(demandPhoto4);
		photoList.add(demandPhoto5);
		photoList.add(demandPhoto6);
		for (MultipartFile photo : photoList) {
			if (!photo.isEmpty()) {
				UserFaceUploadUtil ufuu = new UserFaceUploadUtil();
				String status = ufuu.doUpload(photo, request
						.getServletContext().getRealPath("/"));
				switch (status) {
				case "-2":
					return JSON.toJSONString(new MyMapBuilder(0,
							"对不起，图片尺寸应小于15M").build());

				case "-1":
					return JSON.toJSONString(new MyMapBuilder(0, "对不起，该文件不是图片")
							.build());

				case "0":
					return JSON.toJSONString(new MyMapBuilder(0, "出现未知错误")
							.build());
				default:
					photoPathList.add(status);

				}

			}
		}
		demand.setDemandPhotoUrlJson(JSON.toJSONString(photoPathList));
		demandService.save(demand);
		return JSON.toJSONString(new MyMapBuilder(1, "发布成功").build());
	}

	/**
	 * 获取需求列表 jiangjintai 2016年3月27日
	 * 
	 * @param isAll
	 * @param pageNo
	 * @param size
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDemand")
	@ApiOperation(value = "获取需求列表", httpMethod = "GET")
	public String getDemand(
			@ApiParam(value = "是否是全部1代表本校，0代表全部", name = "isAll", required = true) @RequestParam("isAll") String isAll,
			@ApiParam(value = "起始页0开始", name = "pageNo", required = true) @RequestParam("pageNo") String pageNo,
			@ApiParam(value = "请求阔度", name = "size", required = true) @RequestParam("size") String size,
			@ApiParam(value = "用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId) {
		TbUser user = userService.get(filterUserId);
		System.out.println(user.getUserSchool());
		List<TbDemand> demandList;
		if (isAll.equals("1")) {// 代表本校
			demandList = demandService.getMySchoolDemand(user.getUserSchool(),
					pageNo, size, filterUserId);
		} else {// 代表全部
			demandList = demandService.getAllDemand(pageNo, size, filterUserId);
		}
		return JSON.toJSONString(
				new MyMapBuilder(1, "获取成功").put("demandList", demandList)
						.build(),
				SerializerFeature.DisableCircularReferenceDetect);
	}

	/**
	 * 获取需求细节 jiangjintai 2016年3月27日
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDemandDetail")
	@ApiOperation(value = "获取需求细节", httpMethod = "GET")
	public String getDemandDetail(
			@ApiParam(value = "需求id", name = "demandId", required = true) @RequestParam("demandId") String demandId,
			@ApiParam(value = "用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId) {
		System.out.println(demandId);
		System.out.println(filterUserId);
		TbDemand demand = demandService.getDemandDetail(demandId, filterUserId);
		return JSON.toJSONString(
				new MyMapBuilder(1, "获取成功").put("demand", demand).build(),
				SerializerFeature.DisableCircularReferenceDetect);
	}

	/**
	 * 获取某用户信息
	 */
	@ResponseBody
	@RequestMapping("/getOtherUser")
	@ApiOperation(value = "获取某用户信息", httpMethod = "GET")
	public String getOtherUser(
			@ApiParam(value = "某个用户id", name = "userId", required = true) @RequestParam("userId") String userId) {
		TbUser user = userService.getOtherUserMessage(userId);
		return JSON.toJSONString(new MyMapBuilder(1, "获取成功").put("user", user)
				.build());
	}

	/**
	 * 为某用户点赞或取消赞 jiangjintai 2016年3月29日
	 * 
	 * @param demandId
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/likeTheDemand")
	@ApiOperation(value = "为用户点赞或取消赞", httpMethod = "GET")
	public String likeTheDemand(
			@ApiParam(value = "需求idid", name = "demandId", required = true) @RequestParam("demandId") String demandId,
			@ApiParam(value = "类型，1代表点赞，0代表取消点赞", name = "type", required = true) @RequestParam("type") String type,
			@ApiParam(value = "某个用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId) {
		demandService.doTheLikeOrNot(demandId, type, filterUserId);
		return JSON.toJSONString(new MyMapBuilder(1, "操作成功").build());
	}

	/**
	 * 评论某条需求 jiangjintai 2016年3月29日
	 * 
	 * @param demandId
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addComment")
	@ApiOperation(value = "评论某条内容", httpMethod = "GET")
	public String addComment(
			@ApiParam(value = "某个需求id", name = "demandId", required = true) @RequestParam("demandId") String demandId,
			@ApiParam(value = "内容", name = "content", required = true) @RequestParam("content") String content,
			@ApiParam(value = "某个用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId) {
		demandService.addComment(demandId, content, filterUserId);
		return JSON.toJSONString(new MyMapBuilder(1, "评论成功").build());
	}

	/**
	 * 评论某一条评论 jiangjintai 2016年3月29日
	 * 
	 * @param commentId
	 * @param content
	 * @param filterUserId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addCommentChildComment")
	@ApiOperation(value = "评论某条评论", httpMethod = "GET")
	public String addCommentChildComment(
			@ApiParam(value = "评论id", name = "commentId", required = true) @RequestParam("commentId") String commentId,
			@ApiParam(value = "评论内容", name = "content", required = true) @RequestParam("content") String content,
			@ApiParam(value = "用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId) {
		commentService.addCommentChildComment(commentId, content, filterUserId);
		return JSON.toJSONString(new MyMapBuilder(1, "回复成功").build());

	}

	/**
	 * 发起聊天 jiangjintai 2016年3月29日
	 * 
	 * @param charTo
	 * @param filterUserId
	 * @param message
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addChar")
	@ApiOperation(value = "发起聊天", httpMethod = "GET")
	public String addChar(
			@ApiParam(value = "发送到某个用户id", name = "charTo", required = true) @RequestParam("charTo") String charTo,
			@ApiParam(value = "登录用户的id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId,
			@ApiParam(value = "发送内容", name = "message", required = true) @RequestParam("message") String message) {
		charService.addChar(charTo, filterUserId, message);
		return JSON.toJSONString(new MyMapBuilder(1, "发送成功").build());
	}

	/**
	 * 发起需求请求 jiangjintai 2016年3月29日
	 * 
	 * @param demandId
	 * @param filterUserId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addDemandRequest")
	@ApiOperation(value = "发起请求", httpMethod = "GET")
	public String addDemandRequest(
			@ApiParam(value = "需求id", name = "demandId", required = true) @RequestParam("demandId") String demandId,
			@ApiParam(value = "用户id", name = "filterUserId", required = true) @RequestParam("filterUserId") String filterUserId,
			@ApiParam(value = "请求信息", name = "message", required = true) @RequestParam("message") String message) {
		try {
			demandService.addDemandRequest(demandId, filterUserId, message);
		} catch (Exception e) {
			return JSON.toJSONString(new MyMapBuilder(0, e.getMessage())
					.build());
		}

		return JSON.toJSONString(new MyMapBuilder(1, "发起请求成功").build());
	}

}
